$(function(){
	var basePath=$("#basepath").val();
	//筛选->读取系
	$("#professionFilter_form select[name='college']").change(function(){
		var collegeId=$(this).val();
		var $depSelect=$("#professionFilter_form select[name='department']");
		$depSelect.html("");
		if(collegeId==0){
			$(" <option value='0'>不限</option>").appendTo($depSelect);
			return;
		}
		$.get(basePath+ "admin/branch/getDepsOfCollege/"+collegeId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						if(data.error>0){
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							return;
						}
						$(" <option value='0'>不限</option>").appendTo($depSelect);
						for(i=0;i<data.length;i++){
							$(" <option/>").val(data[i].id).html(data[i].name).appendTo($depSelect);
						}
					}
			}, "json");
	});
	//筛选按钮
	$("#professionFilter_go").click(function(){
		var clgId=$("#professionFilter_form select[name='college']").val();
		var depId=$("#professionFilter_form select[name='department']").val();
		window.location=basePath+"admin/profession/showProfessionList/"+clgId+ "/"+depId+"/1";
	});
	//编辑按钮
	$(".profession_edit").click(function(){
		var proId=$(this).attr("data-professionId");
		$("#profession_edit_box_alert_container").html("");//清空提示框
		$.get(basePath+ "admin/profession/getProfession/"+proId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						if(data.error>0){
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							return;
						}
						var collegeId=data.department.college.id;
						var departmentId=data.department.id;
						$("#profession_edit_box_form input[name='profession_no']").val(data.no);
						$("#profession_edit_box_form input[name='profession_name']").val(data.name);
						$("#profession_edit_box_form input[name='profession_id']").val(data.id);
						$("#profession_edit_box_form select[name='college']").val(collegeId);
						//生成系选择框
						var $depSelect=$("#profession_edit_box_form select[name='department']");
						$depSelect.html("");
						$.get(basePath+ "admin/branch/getDepsOfCollege/"+collegeId,{},
								function(data_dep,textStatus_dep){
									if(textStatus_dep=="success"){
										if(data_dep.error>0){
											alert("登陆状态已过期,请重新登陆");
											window.location=basePath;
											return;
										}
										for(i=0;i<data_dep.length;i++){
											 $(" <option/>").val(data_dep[i].id).html(data_dep[i].name).appendTo($depSelect);
										}
										$depSelect.val(departmentId);
										$("#profession_edit_box .ajax_wait").hide();
										$("#profession_edit_box_form").show();
									}
						},"json");
					}
		},"json");
	});
	//编辑->切换学院
	$("#profession_edit_box_form select[name='college']").change(function(){
		var collegeId=$(this).val();
		var $depSelect=$("#profession_edit_box_form select[name='department']");
		$depSelect.html("");
		$.get(basePath+ "admin/branch/getDepsOfCollege/"+collegeId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						if(data.error>0){
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							return;
						}
						for(i=0;i<data.length;i++){
							 $(" <option/>").val(data[i].id).html(data[i].name).appendTo($depSelect);
						}
						if(data.length==0){
							$(" <option value='0'>不可用</option>").appendTo($depSelect);
							makeAlert("无可用系,请添加之后再试","error",$("#profession_edit_box_alert_container"),"no_dep_error");
						}else{
							$("#profession_edit_box_alert_container .no_dep_error").alert('close');
						}
					}
			}, "json");
	});
	//编辑->保存
	$("#profession_edit_box_save").click(function(){
		var departmentId=$("#profession_edit_box_form select[name='department']").val();
		var proId =$("#profession_edit_box_form input[name='profession_id']").val();
		var proNo =$("#profession_edit_box_form input[name='profession_no']").val();
		var proName =$("#profession_edit_box_form input[name='profession_name']").val();
		if(departmentId==0){
			makeAlert("无可用系,请添加之后再试","error",$("#profession_edit_box_alert_container"),"no_dep_error");
			return;
		}
		$("#profession_edit_box_alert_container .empty_field_error").alert('close');
		$("#profession_edit_box_alert_container .repeat_no_error").alert('close');
		$.post(basePath+ "admin/profession/updateProfession",{id:proId,no:proNo,name:proName,departId:departmentId},
				function(data,textStatus){
					if(textStatus=="success"){
						switch (data) {
						case "-1":
							alert("请勿进行非法操作");
							window.location=basePath;
							break;
						case "-2":
							alert("请勿进行非法操作");
							window.location=basePath;
							break;
						case "-3":
							makeAlert("字段不允许为空,请重新检查","error",$("#profession_edit_box_alert_container"),"empty_field_error");
							break;
						case "-4":
							makeAlert("已存在编号为"+proNo+"的专业,请检查后再输入","error",$("#profession_edit_box_alert_container"),"repeat_no_error");
							break;
						case "-5":
							alert("参数错误,请重试");
							history.go(0);
							break;
						case "0":
							noty({text:"更新成功,正在刷新"});
							$("#profession_edit_box").modal('hide');
							history.go(0);
							break;
						default:
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							break;
						}
					}
			}, "text");
	});
	//删除按钮
	$(".profession_delete").click(function(){
		$("#profession_delete_box input[name='professionId']").val($(this).attr("data-professionId"));
	});
	//删除确认
	$("#profession_delete_box_delete").click(function(){
		var proId=$("#profession_delete_box input[name='professionId']").val();
		$.get(basePath+ "admin/profession/deleteProfession/"+proId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						switch (data) {
						case "-1":
							noty({text:"删除错误:此专业之下还有未删除的专业方向",type:"error"});
							$("#profession_delete_box").modal('hide');
							break;
						case "-2":
							alert("请勿进行非法操作");
							window.location=basePath;
							break;
						case "0":
							noty({text:"删除成功,正在刷新"});
							$("#profession_delete_box").modal('hide');
							history.go(0);
							break;
						default:
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							break;
						}
					}
			}, "text");
	});
	//添加按钮
	$("#profession_add").click(function(){
		$("#profession_add_box_alert_container").html("");
		$("#profession_add_box_form select[name='college']").change();
	});
	//添加->学院切换
	$("#profession_add_box_form select[name='college']").change(function(){
		var collegeId=$(this).val();
		var $depSelect=$("#profession_add_box_form select[name='department']");
		$depSelect.html("");
		$.get(basePath+ "admin/branch/getDepsOfCollege/"+collegeId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						if(data.error>0){
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							return;
						}
						for(i=0;i<data.length;i++){
							 $(" <option/>").val(data[i].id).html(data[i].name).appendTo($depSelect);
						}
						if(data.length==0){
							$(" <option value='0'>不可用</option>").appendTo($depSelect);
							makeAlert("无可用系,请添加之后再试","error",$("#profession_add_box_alert_container"),"no_dep_error");
						}else{
							$("#profession_add_box_alert_container .no_dep_error").alert('close');
						}
					}
			}, "json");
	});
	//添加确认
	$("#profession_add_box_save").click(function(){
		var departmentId=$("#profession_add_box_form select[name='department']").val();
		var proNo =$("#profession_add_box_form input[name='profession_no']").val();
		var proName =$("#profession_add_box_form input[name='profession_name']").val();
		if(departmentId==0){
			makeAlert("无可用系,请添加之后再试","error",$("#profession_add_box_alert_container"),"no_dep_error");
			return;
		}
		$("#profession_add_box_alert_container .empty_field_error").alert('close');
		$("#profession_add_box_alert_container .repeat_no_error").alert('close');
		$.post(basePath+ "admin/profession/insertProfession/",{no:proNo,name:proName,departId:departmentId},
				function(data,textStatus){
					if(textStatus=="success"){
						switch (data) {
						case "-1":
							alert("请勿进行非法操作");
							window.location=basePath;
							break;
						case "-2":
							makeAlert("字段不允许为空,请重新检查","error",$("#profession_add_box_alert_container"),"empty_field_error");
							break;
						case "-3":
							makeAlert("已存在编号为"+proNo+"的专业,请检查后再输入","error",$("#profession_add_box_alert_container"),"repeat_no_error");
							break;
						case "-4":
							alert("参数错误,请重试");
							history.go(0);
							break;
						case "0":
							noty({text:"添加成功,正在刷新"});
							$("#profession_add_box").modal('hide');
							history.go(0);
							break;
						default:
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							break;
						}
					}
			}, "text");
	});
});